import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import store from "../store";
import autoJump from "../Util/autoJump";

// 动态路由
const DynamicmapsRoutes = {
  "/menu/one": {
    // 个人中心
    path: "menu/one",
    component: () => import("@/components/profile.vue"),
    meta: {
      //路由身体上的信息 可以通过 $route 可以拿到
      index1: "首页",
      index2: "个人中心",
    },
  },
  "/menu/two": {
    // 用户管理
    path: "menu/two",
    component: () => import("@/components/userManagement.vue"),
    meta: {
      index1: "首页",
      index2: "用户管理",
    },
  },
  "/menu/three": {
    // 创造资讯
    path: "menu/three",
    component: () => import("@/components/information.vue"),
  },
  "/menu/four": {
    // 互联影库
    path: "menu/four",
    component: () => import("@/components/Allmov.vue"),
  },
  "/menu/five": {
    // 销售排名
    path: "menu/five",
    component: () => import("@/components/SalesEcharts.vue"),
  },
  "/menu/six": {
    // 销售信息
    path: "menu/six",
    component: () => import("@/components/SalesMessage.vue"),
  },
  "/menu/seven": {
    // 员工工资
    path: "menu/seven",
    component: () => import("@/components/empSalary.vue"),
  },
  "/menu/eight": {
    // 高管工资
    path: "menu/eight",
    component: () => import("@/components/richText.vue"),
  },
  "/menu/nine": {
    // 审批信息
    path: "menu/nine",
    component: () => import("@/components/leaveApproval.vue"),
  },
  "/menu/ten": {
    // 审批信息
    path: "menu/ten",
    component: () => import("@/components/leaveApproval.vue"),
  },
  "/menu/eleven": {
    // 员工管理
    path: "menu/eleven",
    component: () => import("@/components/staffManagement.vue"),
  },
  "/menu/twelve": {
    // 请假管理
    path: "menu/twelve",
    component: () => import("@/components/LeaveManagementCeo.vue"),
  },
};

const publicRoutes = [
  {
    path: "/",
    name: "home",
    component: () => import("@/layout/index"),
    redirect: "/menu/one",
    children: [
      {
        path: "menu/one",
        component: () => import("@/components/profile.vue"),
        meta: {
          index1: "首页",
          index2: "个人中心",
        },
      }, //不同的用户进来  插入对应的权限到 chilren的后面
    ],
  },
  {
    path: "/about",
    name: "About",
    component: () => import("../views/About.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
  },
  {
    path: "/:pathMatch(.*)/",
    component: () => import("@/layout/error"),
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes: publicRoutes,
});

// 路由守卫  适配后台管理  特点 不登陆只能在登录页面
const whiteList = ["/login"]; // 白名单
router.beforeEach(async (to, from, next) => {
  // 长时间静默退出
  autoJump(router);

  if (store.getters.token) {
    // console.log("路由守卫中取到token", store.getters.token);
    if (JSON.stringify(store.state.user.userInfo == "{}")) {
      //有token登录了但是此时用户信息不够渲染主页情况下
      await store.dispatch("user/getUserInfo");
    }
    if (to.path == "/login") {
      next();
    } else {
      next();
    }
  } else {
    if (whiteList.indexOf(to.path) > -1) {
      next(); //如果我没登录 但是我要去login页面 让我去
    } else {
      next("/login");
    }
  }
});

// 动态路由权限算法
export const initDynamanicRouter = () => {
  const routes = router.options.routes; // 返回当前的路由
  console.log("routes 当前路由", routes);
  const route = routes.find((item) => item.name == "home"); //找到需要推入的路由
  console.log("route找到需要推入的路由", route);
  if (sessionStorage.getItem("userRights")) {
    // 拿到当前用户的权限
    var rights = JSON.parse(sessionStorage.getItem("userRights"));
  } else {
    rights = [];
  }

  console.log("rights当前用户权限", rights);

  rights.forEach((item) => {
    item.children.forEach((child) => {
      if (DynamicmapsRoutes[child.path]) {
        //根据动态路由表和后台权限比对 动态推入相关路由
        route.children.push(DynamicmapsRoutes[child.path]);
      }
      router.addRoute(route); //路由重置
    });
  });
};

initDynamanicRouter();

export default router;
